package com.ctg.payroll2.service.sysadmin.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ctg.payroll2.bean.po.PbSecgroup;
import com.ctg.payroll2.service.sysadmin.RoleService;
import com.ctg.payroll2.util.PayrollUtil;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.entity.Resource;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;

@SuppressWarnings("unchecked")
public class RoleServiceImpl implements RoleService{
	private SpringSecurityManager springSecurityManager;
	private IbatisEntityDao ibatisEntityDao;

	public void setIbatisEntityDao(IbatisEntityDao ibatisEntityDao) {
		this.ibatisEntityDao = ibatisEntityDao;
	}

	public List<PbSecgroup> getTree(String id) {
		List list = new ArrayList();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList("pbSecgroup.getTree", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<PbSecgroup> getNode(String id){
		List list = new ArrayList();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList("pbSecgroup.getFirstNodeList",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * @param springSecurityManager 设置springSecurityManager
	 */
	public void setSpringSecurityManager(SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}

	public List<Resource> getResourceId(String id) {
		List<Resource> ret = new ArrayList<Resource>();
		List<String> list = new ArrayList<String>();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList("pbSecgroup.getResourceId", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ret = springSecurityManager.getPrivilegesByPrivilegeIds(PayrollUtil.getStringArrayByList(list));
		return ret;
	}
	
	public Page getResourcePageId(String id) {
		List<String> list = new ArrayList<String>();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList("pbSecgroup.getResourceId", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return springSecurityManager.getPrivilegesPageByPrivilegeIds(PayrollUtil.getStringArrayByList(list));
	}
}
